<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-11-28 14:48
 *
 * Version: 2.0
 */

namespace App\Modules\RedBag\Repositories;


use App\Models\RedBagList;
use Flower\Core\Repository\Eloquent\BaseRepository;
use function foo\func;

class RedBagListRepository extends BaseRepository
{
	public function model()
	{
		return RedBagList::class;
	}

	/**
	 * 抢红包
	 * @param $redBagInfo
	 * @param $memberId
	 * @param $source
	 * @return mixed
	 */
	public function addItem($redBagInfo, $memberId, $source,$liveId)
	{
		$data = [
			'store_id'  => $redBagInfo->store_id,
			'coupon_id' => $redBagInfo->id,
			'code'      => 0,
			'member_id' => $memberId,
			'send_time' => date('Y-m-d H:i:s'),
			'status'    => 0,
			'source'    => $source,
			'created_at' => date('Y-m-d H:i:s'),
			'live_id'   => $liveId
		];
		return $this->model->insertGetId($data);
	}


	/**
	 * 获取列表
	 * @param $redBagId
	 * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model[]
	 */
	public function getListByRedBagId($redBagId,$liveId){
		return  $this->model->select('id','member_id','coupon_id')->with('member:id,name,face','redBag:id,money')->where(['coupon_id'=>$redBagId,'live_id'=>$liveId])->get();
	}


	/**
	 * 一个红包的用户列表
	 * @param $redBagId
	 * @return mixed
	 */
	public function getMemberListByRedBagId($redBagId){
		return $this->model->select('id','member_id','coupon_id')->with('member:id,name,face','redBag:id,money')->where(['coupon_id'=>$redBagId])->whereDate('created_at','=',date('Y-m-d'))->get();
	}


	/**
	 * 当前直播间用户自己的抢到的红包列表
	 * @param $userId
	 * @param $memberId
	 * @return mixed
	 */
	public function getMemberListSelfByRedBagIdAndUserId($userId,$memberId,$liveId){
		return $this->model->select('id','member_id','coupon_id')->with('member:id,name,face')->with(['redBag'=>function($v)use($userId){
			$v->select('id','money')->where(['user_id'=>$userId]);
		}])->where(['member_id'=>$memberId,'live_id'=>$liveId])->whereDate('created_at','=',date('Y-m-d'))->get();
	}
}